﻿using System;
using System.Data;
using System.Globalization;
using System.Web.UI.WebControls;
using Pantry.Business;
using Pantry.Commons;

namespace Pantry.Web.Users.Controls
{
    public partial class ListDistrict : System.Web.UI.UserControl
    {
        private static DataView dataView;
        private static bool isSort = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            var strTrustRegionId = Request.QueryString["TrustRegionId"];
            if (!Validates.IsNaturalNumber(strTrustRegionId))
            {
                return;
            }

            var trustRegionId = Convert.ToInt32(strTrustRegionId,CultureInfo.CurrentCulture);
            var trustDistrictBus = new TrustDistrictBus();
            var listDistricts = trustDistrictBus.RetrieveTrustDistrictsByRegion(trustRegionId);
            if (listDistricts == null)
            {
                // Occur SQL Server error
                return;
            }

            dataView = listDistricts.DefaultView;
            FilterIsActive(CheckBoxIncludeActivate.Checked);
            CollectionPager.PageSize = 10;
            CollectionPager.DataSource = dataView;
            CollectionPager.BindToControl = RepeaterDistricts;
            RepeaterDistricts.DataSource = CollectionPager.DataSourcePaged;
            RepeaterDistricts.DataBind();
        }

        public void ButtonCreateClicked(object sender, EventArgs eventArgs)
        {
            if (Request.QueryString["TrustRegionId"] != null)
                Response.Redirect("DistrictDetail.aspx?TrustRegionId=" + Request.QueryString["TrustRegionId"]);
        }

        public void LinkButtonClicked(object sender, EventArgs eventArgs)
        {
            var button = (LinkButton)sender;
            Sort(button.ID);
        }

        public void ButtonFilterAllClicked(object sender, EventArgs eventArgs)
        {
            var strFillter = string.Empty;
            Fillter(strFillter);
        }

        public void ButtonFilter0Clicked(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TrustDistrictName LIKE '0%'
                OR TrustDistrictName LIKE '1%'
                OR TrustDistrictName LIKE '2%'
                OR TrustDistrictName LIKE '3%'
                OR TrustDistrictName LIKE '4%'
                OR TrustDistrictName LIKE '5%'
                OR TrustDistrictName LIKE '6%'
                OR TrustDistrictName LIKE '7%'
                OR TrustDistrictName LIKE '8%'
                OR TrustDistrictName LIKE '9%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterAClicked(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TrustDistrictName LIKE 'A%'
                OR TrustDistrictName LIKE 'B%'
                OR TrustDistrictName LIKE 'C%'
                OR TrustDistrictName LIKE 'D%'
                OR TrustDistrictName LIKE 'E%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterFClicked(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TrustDistrictName LIKE 'A%'
                OR TrustDistrictName LIKE 'F%'
                OR TrustDistrictName LIKE 'G%'
                OR TrustDistrictName LIKE 'H%'
                OR TrustDistrictName LIKE 'I%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterKClicked(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TrustDistrictName LIKE 'K%'
                OR TrustDistrictName LIKE 'L%'
                OR TrustDistrictName LIKE 'M%'
                OR TrustDistrictName LIKE 'N%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterOClicked(object sender, EventArgs eventArgs)
        {
            const string strFilter = @"
                TrustDistrictName LIKE 'O%'
                OR TrustDistrictName LIKE 'P%'
                OR TrustDistrictName LIKE 'Q%'
                OR TrustDistrictName LIKE 'R%'
            ";
            Fillter(strFilter);
        }

        public void ButtonFilterSClicked(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TrustDistrictName LIKE 'S%'
                OR TrustDistrictName LIKE 'T%'
                OR TrustDistrictName LIKE 'U%'
                OR TrustDistrictName LIKE 'V%'
            ";
            Fillter(strFillter);
        }

        public void ButtonFilterWClicked(object sender, EventArgs eventArgs)
        {
            const string strFillter = @"
                TrustDistrictName LIKE 'W%'
                OR TrustDistrictName LIKE 'X%'
                OR TrustDistrictName LIKE 'X%'
                OR TrustDistrictName LIKE 'Y%'
                OR TrustDistrictName LIKE 'Z%'
            ";
            Fillter(strFillter);
        }

        private void Fillter(string fillter)
        {
            var strFillter = "IsActive = 'true'";
            if (CheckBoxIncludeActivate.Checked)
            {
                strFillter = "";
            }

            if (!String.IsNullOrEmpty(fillter) & !String.IsNullOrEmpty(strFillter))
            {
                strFillter += " AND ";

            }
            strFillter += fillter;
            dataView.RowFilter = strFillter;
        }

        private static void Sort(string field)
        {
            try
            {
                if (isSort)
                {
                    dataView.Sort = string.Format(CultureInfo.InvariantCulture, "{0} DESC", field);
                    isSort = false;
                }
                else
                {
                    dataView.Sort = string.Format(CultureInfo.InvariantCulture, "{0} ASC", field);
                    isSort = true;
                }
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        private static void FilterIsActive(bool includeIsActive)
        {
            dataView.RowFilter = includeIsActive ? null : "IsActive = 'Yes'";
        }
    }
}